package org.example.controller;


import com.google.common.collect.ImmutableMap;

import lombok.val;
import org.example.dto.RoleDto;
import org.example.dto.SysRoleDto;
import org.example.entity.SysRole;
import org.example.model.DeptModel;
import org.example.model.MenuModel;
import org.example.model.RoleModel;
import org.example.service.SysMenuService;
import org.example.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysMenuService sysMenuService;

    @GetMapping
    public List<SysRole> getAll(){
        return sysRoleService.getAll();
    }

    @GetMapping("/menu/{id}")
    public List<SysRole> getMenuByRole(@PathVariable("id") Integer id){
//        System.out.println("controller-role-menu-byId----->"+sysRoleService.getMenuByRole(id));

        return sysRoleService.getMenuByRole(id);
    }


    //先删除再批量插入
    //插入：一个数组
    @PostMapping("/delAndinsert")
    public void delAndInsertMenuByRole(@RequestBody RoleDto roleDto){
        System.out.println("controller-ids数组数据："+roleDto.getIds());

        sysRoleService.delMenuByRole(roleDto);

    }

    @GetMapping({"/get/{flag}"})
    public List<MenuModel> getRoleInMenuByTree(Principal principal,@PathVariable("flag") String flag ) {
        System.out.println("controller-flag数据为：---------》》true"+ flag);
        if(flag=="true"){
//            System.out.println("sysRoleService.getRoleTree(name)----->"+sysRoleService.getRoleTree(principal.getName()));
            return sysRoleService.getRoleTree(principal.getName());

        }
        else if(flag=="false"){
            String name = "";
//            System.out.println("sysRoleService.getRoleTree(name)----->"+sysRoleService.getRoleTree(name));
            return sysRoleService.getRoleTree(name);
        }
        else{
            String name = "";
//            System.out.println("sysRoleService.getRoleTree(name)----->"+sysRoleService.getRoleTree(name));
            return sysRoleService.getRoleTree(name);


        }

    }

    @PostMapping
    public Map<String, Object> addRole(@RequestBody SysRoleDto sysRoleDto){


        sysRoleService.addRole(sysRoleDto);
        return ImmutableMap.of("code", 1, "msg", "success");
    }

    @GetMapping("/{id}")
    public SysRole getById(@PathVariable("id") Integer id){
        return sysRoleService.getById(id);
    }

    @PutMapping
    public Map<String, Object> editRole(@RequestBody SysRoleDto sysRoleDto){
        sysRoleService.editRole(sysRoleDto);
        return ImmutableMap.of("code", 1, "msg", "success");
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Long id){

        sysRoleService.deleteById(id);

        return ImmutableMap.of("code", 1, "msg", "success");
    }

}
